using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using SimpleServiceBus.Bus.Pipeline;
using SimpleServiceBus.Bus.Pipeline.PipelineComponents;
using SimpleServiceBus.Bus.Pipeline.PipelineComponents.MessageFiltering;
using SimpleServiceBus.Bus.Pipeline.PipelineComponents.MessageLogging;
using SimpleServiceBus.Bus;

namespace SimpleServiceBus.Endpoints.ActiveMQ.Transport {

    public class ActiveMQMessagePipelineFactory: DefaultMessagePipelineFactory {

        //-----< Fields >-----
        //----------------------------------------------------------------------------------        


        //-----< Constructors >-----
        //----------------------------------------------------------------------------------        
        public ActiveMQMessagePipelineFactory() {
        }

        //-----< Public and protected properties >-----
        //----------------------------------------------------------------------------------        

        /// <summary>
        /// Specifies that a receive pipeline should not have any message dispatch component.
        /// </summary>
        /// <remarks>This is used when receiving the response to a request, when invoking <see cref="IExtendedMessageBus.Ask"/></remarks>
        public Boolean IgnoreMessageDispatch {
            get;
            set;
        }

        //-----< Public and protected methods >-----
        //----------------------------------------------------------------------------------        

        public override IMessagePipeline CreateReceivePipelineFor(MessageEnvelope messageEnvelope) {
            var _components = new List<IMessagePipelineComponent>();

            _components.Add(new ActiveMQMessageBodySerializationComponent());
            _components.Add(new MessageHeaderGeneratorComponent());
            _components.Add(new MessageFilterComponent());
            _components.Add(new MessageLoggingComponent());
            if(!IgnoreMessageDispatch) {
                _components.Add(new LocalCallbackDispatcherComponent());
                _components.Add(new MessageHandlerDispatcherComponent());
            }

            var messagePipeline = CreatePipeline(messageEnvelope, PipelineDirection.Receive, _components.ToArray());

            return messagePipeline;
        }

        public override IMessagePipeline CreateSendPipelineFor(MessageEnvelope messageEnvelope) {
            var components = new IMessagePipelineComponent[]
                                 {
                                     new MessageHeaderGeneratorComponent(),
                                     new MessageFilterComponent(),
                                     new MessageLoggingComponent(),
                                     new ActiveMQMessageBodySerializationComponent()
                                 };

            IMessagePipeline messagePipeline = CreatePipeline(messageEnvelope, PipelineDirection.Send, components);
            return messagePipeline;
        }

        //-----< Events >-----
        //----------------------------------------------------------------------------------        


        //-----< Internal members >-----
        //----------------------------------------------------------------------------------        


        //-----< Private members >-----
        //----------------------------------------------------------------------------------        


        //-----< All nested types >-----
        //----------------------------------------------------------------------------------        


        //-----< All explicit interface implementations >-----
        //----------------------------------------------------------------------------------        


    }
}

